<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Models\Admin;
use App\Services\SmsService;
use Dingo\Api\Http\Request;

class AuthController extends BaseController
{
    public function login(Request $request)
    {
        $input = $request->only(['username', 'password']);
        $this->validate($request, [
            'username'   => 'required',
            'password'  => 'required',
        ], [
            'username.required' => '手机号为空',
            'password.required' => '密码为空'
        ]);
        if (! $token = auth('admin')->attempt($input)) {
            return $this->failed("手机号或者密码错误");
        }
        return $this->respondWithToken($token);
    }

    public function permission(Request $request) {
        $user = $this->getUser();
        return $this->success(compact('user'));
    }

    /**
     * Get the authenticated User.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function me()
    {
        return response()->json(auth()->user());
    }

    /**
     * Log the user out (Invalidate the token).
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout()
    {
        auth()->logout();

        return $this->success(['message' => 'Successfully logged out']);
    }

    /**
     * Refresh a token.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function refresh()
    {
        return $this->respondWithToken(auth()->refresh());
    }



}
